//
//  main.m
//  06-Foundation-Dictionary
//
//  Created by Jason on 15/10/4.
//  Copyright © 2015年 Jason. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contact.h"
void dictUse();
void dictUse2();


/*
 字典和set都是无序的
 */
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSMutableArray *contacts = [NSMutableArray array];
        
        // 添加第1个人
        Contact *c1 = [[Contact alloc] init];
        c1.name = @"Mike";
        c1.phone = @"2323231123";
        c1.age = 20;
        c1.sex = Woman;
        [contacts addObject:c1];
        [c1 release];
        
        // 添加第2个人
        Contact *c2 = [[[Contact alloc] init] autorelease];
        c2.name = @"Jack";
        c2.phone= @"123121123";
        c2.age = 30;
        c2.sex = Man;
        [contacts addObject:c2];
        
        
        
        
        /*
         
         利用数组和字典来封装数据
         
        NSMutableArray *contacts = [NSMutableArray array];
        
        // 添加第1个联系人
        NSMutableDictionary *contact1 = [NSMutableDictionary dictionary];
        [contact1 setObject:@"Mike" forKey:@"name"];
        [contact1 setObject:@"123123" forKey:@"phone"];
        [contacts addObject:contact1];
        
        // 添加第2个联系人
        NSMutableDictionary *contact2 = [NSMutableDictionary dictionary];
        [contact2 setObject:@"Jack" forKey:@"name"];
        [contact2 setObject:@"3333232" forKey:@"phone"];
        [contacts addObject:contact2];

         */
        
    }
    return 0;
}

# pragma mark 可变字典
void dictUse4() {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"Mike" forKey:@"name"];
    [dict setObject:@"Jack" forKey:@"name"]; // 覆盖掉mike
    
    [dict setObject:@"北京" forKey:@"address"];
    
    //        NSLog(@"%@", dict);
    
    // 删除
    //        [dict removeObjectForKey:@"name"];
    //        NSLog(@"%@", dict);
}



void dictUse3() {
    
    /*
     1.无序的
     2.以键值对的形式存储数据
     3.比可变
     */
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Mike" ,@"name",
                          @"北京", @"address",
                          @"20", @"age",
                          @"男",@"hobby",
                          @"男",@"sex",
                          nil];
    
    
    // 字典遍历
    /*
     [dict enumerateKeysAndObjectsUsingBlock:
     ^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
     
     NSLog(@"%@====%@",key, obj);
     
     
     }];
     
     */
    
    /*
     for (id key in dict) {
     id obj = [dict objectForKey:key];
     NSLog(@"%@====%@",key, obj);
     }
     
     */
    
    [dict allKeys]; // 获取所有key
    
    [dict allValues]; // 获取所有value
    
}

void dictUse2() {
    
    NSArray *keys = [NSArray arrayWithObjects:@"name",@"address", nil];
    NSArray *values = [NSArray arrayWithObjects:@"Mike",@"北京", nil];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSLog(@"%@", dict);
}

void dictUse() {
    /*
     name:Mike
     address:北京
     age:20
     hobby:女
     sex:男
     */
    // 字典中不允许有两个相同的key,但是允许2个相同的value
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Mike", @"name",
                          @"北京", @"address",
                          @"20", @"age",
                          @"女", @"hobby",
                          @"男", @"sex",
                          nil];
    
    id obj = [dict objectForKey:@"sex"];
    NSLog(@"%@", obj);
    
    NSUInteger count = dict.count; // 计算有多少个键值对 5
    
    /*
     NSDictionary *dict = [NSDictionary dictionaryWithObject:@"Jack" forKey:@"name"];
     
     id obj = [dict objectForKey:@"name"];
     NSLog(@"%@",obj);
     
     */
}
